//
//  SunInputVC.m
//  Xspcf
//
//  Created by sunshine on 16/10/30.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunInputVC.h"
#import "SunWebVC.h"
#import "SunRecordVC.h"
#import "SunDelegateWebVC.h"

@interface SunInputVC ()<UITextFieldDelegate>
{
    UILabel *_leaveMoney;
    UIImageView *_bankLogo;
    UILabel *_bankName;
    UITextField *_bankPhone;
    UITextField *_money;
    UIButton *_next;
    UIButton *_delegate;
    UIView *_payView;
    NSTimer *_timer;
    int _time;
    NSString *_tradeNo;
    NSDictionary *_data;
}
@end

@implementation SunInputVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND;
    self.title = @"我要充值";
    _data = [Sun getUser:USER];
    [self creatUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestToUser];
}

- (void)creatUI{
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, X(25), 25)];
    [btn setImage:[UIImage imageNamed:@"tool_record"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(gotoRecord) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = barbutton;
    
    UIView *cardImageView = [[UIView alloc]init];
    cardImageView.backgroundColor = WHITE;
    [cardImageView setFrame:CGRectMake(0, -9, SCREENWIDTH, 87)];
    [self.view addSubview:cardImageView];
    
    _bankLogo = [[UIImageView alloc]init];
    [cardImageView addSubview:_bankLogo];
    [_bankLogo setFrame:CGRectMake(X(48), 23, X(42), X(42))];
    [_bankLogo sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KIMG,[_data objectForKey:@"bankImg"]]]];
    _bankName = [[UILabel alloc]init];
    [cardImageView addSubview:_bankName];
    [_bankName setFrame:CGRectMake(X(111), 21, 184, 25)];
    _bankName.textColor = [UIColor colorWithHexString:@"565D66"];
    _bankName.font = [UIFont systemFontOfSize:18];
    _bankName.text = [NSString stringWithFormat:@"%@(尾号%@)",[_data objectForKey:@"bank"],[[_data objectForKey:@"bankCard"] substringFromIndex:[[_data objectForKey:@"bankCard"]length] - 4]];
    UILabel *content = [[UILabel alloc]init];
    [cardImageView addSubview:content];
    [content setFrame:CGRectMake(X(111), 47, 271, 17)];
    content.textColor = [UIColor colorWithHexString:@"ABABAB"];
    content.font = [UIFont systemFontOfSize:12];
    content.text = [NSString stringWithFormat:@"单笔限额%d万元，每日限额%d万元。",[[_data objectForKey:@"perLimit"]intValue]/10000,[[_data objectForKey:@"dayLimit"]intValue]/10000];
    
    UIView *inputImageView = [[UIView alloc]init];
    inputImageView.backgroundColor = WHITE;
    [inputImageView setFrame:CGRectMake(0, 111, SCREENWIDTH, 80)];
    [self.view addSubview:inputImageView];
    UILabel *inputWord = [[UILabel alloc]initWithFrame:CGRectMake(X(19), 17, 84, 22)];
    inputWord.text = @"充值金额";
    inputWord.textColor = [UIColor colorWithHexString:@"050505"];
    inputWord.font = [UIFont systemFontOfSize:16];
    [inputImageView addSubview:inputWord];
    
    UIImageView *lineY = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"invest_line"]];
    [inputImageView addSubview:lineY];
    [lineY lt_YcenterTo:inputWord save:YES];
    [lineY lt_setLeft:0 to:inputWord save:YES];
    [lineY lt_setSize:CGSizeMake(1, 38) save:YES];
    
    UILabel *lineX = [[UILabel alloc]init];
    [inputImageView addSubview:lineX];
    lineX.backgroundColor = [UIColor colorWithHexString:@"E1E1E1"];
    [lineX lt_setTop:13 to:inputWord save:YES];
    [lineX lt_setSize:CGSizeMake(X(342), 1) save:YES];
    [lineX lt_XcenterTo:nil save:YES];
    
    _money = [[UITextField alloc]init];
    [inputImageView addSubview:_money];
    [_money setFrame:CGRectMake(X(116), 17, 126, 25)];
    _money.clearButtonMode = UITextFieldViewModeWhileEditing;
    _money.keyboardType = UIKeyboardTypeDecimalPad;
    _money.placeholder = @"请输入充值金额";
    _money.delegate = self;
    inputImageView.userInteractionEnabled = YES;
    
    UILabel *balanceWord = [[UILabel alloc]initWithFrame:CGRectMake(X(19), 56, 77, 20)];
    balanceWord.text = @"账户余额";
    balanceWord.textColor = [UIColor colorWithHexString:@"A2A2A2"];
    balanceWord.font = [UIFont systemFontOfSize:12];
    [inputImageView addSubview:balanceWord];
    
    _leaveMoney = [[UILabel alloc]init];
    [inputImageView addSubview:_leaveMoney];
    [_leaveMoney setFrame:CGRectMake(X(105), 56, 200, 20)];
    _leaveMoney.textColor = [UIColor colorWithHexString:@"FFA887"];
    _leaveMoney.font = [UIFont systemFontOfSize:12];
    _leaveMoney.text = [NSString stringWithFormat:@"%@元",[[Sun getUser:USER] objectForKey:@"balanceMoney"]];
    
    _next = [[UIButton alloc]init];
    [self.view addSubview:_next];
    [_next lt_Top:SunEdgesMake(78, X(46), X(43), 44) to:inputImageView save:YES];
    _next.clipsToBounds = YES;
    [_next setBackgroundImage:[UIImage imageNamed:@"button_bg"] forState:UIControlStateNormal];
    _next.layer.cornerRadius = 4;
    [_next setTitle:@"确定充值" forState:UIControlStateNormal];
    [_next setTitleColor:WHITE forState:UIControlStateNormal];
    _next.titleLabel.font = [UIFont systemFontOfSize:18];
    [_next addTarget:self action:@selector(gotoGetMoney) forControlEvents:UIControlEventTouchUpInside];
    _delegate = [[UIButton alloc]init];
    [self.view addSubview:_delegate];
    [_delegate lt_setTop:16 to:_next save:YES];
    [_delegate lt_setSize:CGSizeMake(X(17), 17) save:YES];
    [_delegate lt_setLeft:X(49) to:nil save:YES];
    [_delegate setImage:[UIImage imageNamed:@"tool_select"] forState:UIControlStateSelected];
    [_delegate setImage:[UIImage imageNamed:@"tool_unselect"] forState:UIControlStateNormal];
    _delegate.selected = YES;
    [_delegate addTarget:self action:@selector(acceptDelegate:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *ll = [[UILabel alloc]init];
    [self.view addSubview:ll];
    [ll lt_YcenterTo:_delegate save:YES];
    [ll lt_setLeft:X(9) to:_delegate save:YES];
    ll.textColor = [UIColor colorWithHexString:@"B2B1B1"];
    ll.font = [UIFont systemFontOfSize:12];
    ll.text = @"我已阅读并同意";
    UIButton *webBtn = [[UIButton alloc]init];
    [self.view addSubview:webBtn];
    [webBtn lt_YcenterTo:ll save:YES];
    [webBtn lt_setLeft:0 to:ll save:YES];
    webBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [webBtn setTitle:@"《小算盘用户充值协议》" forState:UIControlStateNormal];
    [webBtn setTitleColor:[UIColor colorWithHexString:@"B2B1B1"] forState:UIControlStateNormal];
    [webBtn addTarget:self action:@selector(gotoDelegate) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *safeImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"safe_icon"]];
    [safeImage setFrame:CGRectMake(X(108),550, 12, 14)];
    [self.view addSubview:safeImage];
    
    UILabel *safeLable = [[UILabel alloc]initWithFrame:CGRectMake(X(125), 548, 200, 17)];
    safeLable.text = [Sun getUser:SAFETEXT];
    safeLable.textColor = [UIColor colorWithHexString:@"000000"];
    safeLable.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:safeLable];
}

- (void)hideAlert{
    [[Sun shareSun]hideAll];
}

- (void)gotoRecord{
    SunRecordVC *vc = [[SunRecordVC alloc]init];
    vc.type = 2;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onTimer{
    UIButton *btn = [_payView viewWithTag:8];
    btn.enabled=NO;
    if(_time == 0){
        btn.enabled=YES;
        [btn setTitle:@"重发" forState:UIControlStateNormal];
        [_timer invalidate];
        _timer = nil;
        _time = 60;
        return;
    }
    _time --;
    [btn setTitle:[NSString stringWithFormat:@"%ds",_time] forState:UIControlStateNormal];
}

- (void)pay{
        [[Sun shareSun]showActivity:self];
        NSString *url = [NSString stringWithFormat:@"%@%@",KURL,API_RECHARGE];
    NSDictionary *dic = @{@"money":_money.text,@"device":@"iOS"};

    // 加密
    NSString *str = [Sun stringFromDictionary:(NSDictionary *)dic];
    // 界面
    SunDelegateWebVC *vc = [[SunDelegateWebVC alloc]init];
    vc.params = str;
    vc.urlStr = url;
    vc.type = Recharge;
    vc.title = @"充值";
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)requestToUser{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",KURL,API_USERINFO];
    [HttpRequest postURL:urlStr withParam:nil success:^(id requestDic, NSString *msg) {
        [Sun setUser:requestDic forKey:USER];
        _data = requestDic;
//        _leaveMoney.text = [requestDic objectForKey:@"balanceMoney"];
        _leaveMoney.text = [NSString stringWithFormat:@"%.2f元",[[requestDic objectForKey:@"balanceMoney"] floatValue]];
    } failure:^(NSString *errorInfo, int code) {
        [[Sun shareSun]showMsg:errorInfo];
    }];
}

- (void)gotoDelegate{
    SunWebVC *vc = [[SunWebVC alloc]init];
    vc.urlStr = [NSString stringWithFormat:@"%@/More/recharge_protocol?source=ios",KWAP];
    vc.title = @"用户充值协议";
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)acceptDelegate:(UIButton *)btn{
    if (btn.isSelected) {
        btn.selected = NO;
    }else {
        btn.selected = YES;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString *result = textField.text;
    if (string.length > 0 && textField.text.length < 6) {
        result = [textField.text stringByAppendingString:string];
    }else if (string.length <= 0 && result.length > 0){
        result = [textField.text substringToIndex:textField.text.length - 1];
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    textField.text = [NSString stringWithFormat:@"%d",textField.text.intValue];
    if (textField.text.doubleValue == 0) {
        textField.text = @"";
    }
}

- (void)gotoGetMoney{
    if (_money.text.floatValue == 0) {
        [[Sun shareSun]showMsg:@"请输入充值金额"];
        return;
    }else if (!_delegate.isSelected){
        [[Sun shareSun]showMsg:@"请阅读并同意《小算盘用户充值协议》"];
        return;
    }else {
        [self pay];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
